﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CTManagerSoln
{
    public partial class FrmDonviPhongban : Form
    {
        DataTable dt;
        private bool isUpdate;
        public FrmDonviPhongban()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.TopMost = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmDonviPhongban_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add("Maphongban");
            dt.Columns.Add("Tenphongban");
            dt.Columns.Add("Mota");
            LoadGrid();
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            //string path1 = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            var directory = System.IO.Path.GetDirectoryName(path);
            string Err = string.Empty;
            string maphongban = txtmaphongban.Text;
            string tenphongban = txttenphongban.Text;
            string mota = txtghichu.Text;
            if (string.IsNullOrEmpty(maphongban))
            {
                Err += "- Mã loại chứng từ chưa nhập. \n";
            }
            if (string.IsNullOrEmpty(tenphongban))
            {
                Err += "- Tên loại chứng từ chưa nhập. \n";
            }

            if (Err.Length > 5)
            {
                MessageBox.Show(Err, "Thông báo");
            }
            else
            {
                if (clsPhongban.GetPhongban(maphongban).Rows.Count < 1 && isUpdate)
                {
                    DialogResult result = MessageBox.Show("thêm mới phòng ban.", "Thông báo.",MessageBoxButtons.OKCancel);
                    if (result == DialogResult.OK)
                    {
                        clsPhongban.Insert(maphongban, tenphongban, mota);
                    }
                }
                else
                {
                    if (isUpdate)
                    {
                        clsPhongban.Update(maphongban, tenphongban, mota);
                    }
                    else
                    {
                        clsPhongban.Insert(maphongban, tenphongban, mota);
                    }                    
                }
                LoadGrid();
                resetInput();
                isUpdate = false;
            }
        }

        private void resetInput()
        {
            txtghichu.Text = "";
            txtmaphongban.Text = "";
            txttenphongban.Text = "";
        }

        private void LoadGrid()
        {
            dt = new DataTable();
            dt = clsPhongban.GetAllPhongban();
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            int RowIndex = dataGridView1.SelectedCells[0].RowIndex;

            // get primary key base on row col index
            string maphongban = dataGridView1["MaPhongban", RowIndex].FormattedValue.ToString();
            string tenphongban = dataGridView1["TenPhongban", RowIndex].FormattedValue.ToString();
            string mota = dataGridView1["Mota", RowIndex].FormattedValue.ToString();
            if (!string.IsNullOrEmpty(maphongban) && !string.IsNullOrEmpty(tenphongban))
            {
                txtmaphongban.Text = maphongban;
                txttenphongban.Text = tenphongban;
                txtghichu.Text = mota;
                isUpdate = true;
            }
        }
    }
}